#include "Particle.h"
#include "Timer.h"
#include <glut.h>

Particle::Particle(Vec3f Pos, float mass)
{
	if(mass != 0)
	{
		m_InverseMass = 1 / mass;
	}
	if(mass < 0)
	{
		m_InverseMass = 0;
	}

	m_Pos = Pos;
}

void Particle::UpdateEuler()
{
	m_Acc = m_Combinedforce * m_InverseMass;
	float dt  = TheTimer::Instance()->GetDeltaTime();
	Vec3f oldVel = m_Vel;
	m_Vel += m_Acc * dt;
	m_Vel * 0.99;
	m_Pos += (oldVel + m_Vel) * 0.5f * dt;
	m_Combinedforce = Vec3f();
}

void Particle::UpdateVerlet()
{
	m_Acc = m_Combinedforce * m_InverseMass;
	float dt  = TheTimer::Instance()->GetDeltaTime();
	Vec3f diff = m_Pos - m_oldPos + m_Acc * dt * dt;
	m_oldPos = m_Pos;
	m_Pos += diff;
	m_Combinedforce = Vec3f();
}

void Particle::Draw()
{
	glPushMatrix();
		glTranslatef(m_Pos.m_x, m_Pos.m_y, m_Pos.m_z);
		glutSolidSphere(0.1, 16, 16);
	glPopMatrix();
}

void Particle::ApplyForce(Vec3f Force)
{
	m_Combinedforce += Force;
}

void Particle::Init(Vec3f Pos, float mass)
{
	if(mass != 0)
	{
		m_InverseMass = 1 / mass;
	}
	if(mass == -1)
	{
		m_InverseMass = 0;
	}
	m_Pos = Pos;
}